* Construct imported input shares at the sector level from OECD

clear
set more off
local countries "AUS AUT BEL CAN CHL CZE DNK EST FIN FRA DEU GRC HUN ISL IRL ISR ITA JPN LUX NLD NZL NOR POL PRT SVK SVN ESP SWE CHE GBR USA BGR BRN CHN CRI CYP HKG HRV IND KHM LTU LVA MLT PHL ROU SAU SGP TUN TWN VNM ZAF RoW BRA RUS IDN KOR TUR MYS ARG COL MEX THA"
foreach cc of local countries {
foreach t of numlist 1995(1)2011 {
import delimited using "OECD/input/`cc'`t'domimp", clear
drop cons gfcf invnt cons_nonres cons_abr expo gdp
gen var = _n
drop if var>68
gen dom = 0
replace dom =1 if var<=34
gen imp = 0
replace imp =1 if var>=35 
local jj =1
local nombres "c01t05 c10t14 c15t16 c17t19 c20 c21t22 c23 c24 c25 c26 c28 c27 c29 c30t33x c31 c34 c35 c36t37 c40t41 c45 c50t52 c55 c60t63 c64 c65t67 c70 c71 c72 c73t74 c75 c80 c85 c90t93 c95"
foreach x of local nombres  {
bys dom: egen s`jj' = total(`x') // spending by local or imported, for each sector (column)
local jj = `jj'+1
}
drop c01t05 c10t14 c15t16 c17t19 c20 c21t22 c23 c24 c25 c26 c28 c27 c29 c30t33x c31 c34 c35 c36t37 c40t41 c45 c50t52 c55 c60t63 c64 c65t67 c70 c71 c72 c73t74 c75 c80 c85 c90t93 c95
bys dom: gen counter = _n
keep if counter==1
drop counter var v1 dom
reshape long s, i(imp) j(sector)
sort sector
bys sector: egen tots  = total(s)
gen importshare = s / tots
keep if imp==1
drop imp s
egen aggspend = total(tots)
gen mi = tots / aggspend
drop aggspend tots
gen year=`t'
gen Country="`cc'"
save "OECD/temp/`cc'`t'_sector.dta", replace
}
use "OECD/temp/`cc'1995_sector.dta"
foreach t of numlist 1996(1)2011 {
append using "OECD/temp/`cc'`t'_sector.dta"
}
save "OECD/temp/`cc'_append_sector.dta", replace
}
local countries2 "AUS AUT BEL CAN CHL CZE DNK EST FIN FRA DEU GRC HUN ISL IRL ISR ITA JPN LUX NLD NZL NOR POL PRT SVK SVN ESP SWE CHE GBR USA BGR BRN CHN CRI CYP HKG HRV IND KHM LTU LVA MLT PHL ROU SAU SGP TUN TWN VNM ZAF RoW BRA RUS IDN KOR TUR MYS ARG COL MEX"
foreach cc of local countries2 {
append using "OECD/temp/`cc'_append_sector.dta"
}
ren Country ccode
sort ccode year
save "OECD/output/IMPORTSHARES_OECD_inputs_allcountries_sector.dta", replace
